package org.darkforest.controller;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.darkforest.dao.mapper.CadreSupportPoorMapper;
import org.darkforest.dao.mapper.CmsSpecialMapper;
import org.darkforest.dao.mapper.CmsSpecialCommentMapper;
import org.darkforest.dao.mapper.CmsSpecialCommentUpMapper;
import org.darkforest.dao.mapper.CountySupportPoorMapper;
import org.darkforest.model.CadreSupportPoor;
import org.darkforest.model.CmsSpecialWithBLOBs;
import org.darkforest.model.CountySupportPoor;
import org.darkforest.model.RestEntity;
import org.darkforest.model.CmsSpecialComment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 扶贫管理
 *
 * @auth bingo
 * @create 2017-11-22-18:47
 */
@RestController
@RequestMapping(value = "supportPoor")
@Api(value = "supportPoor", description = "扶贫风采", produces = MediaType.APPLICATION_JSON_VALUE)
public class SupportPoorController {
    @Autowired
    private CountySupportPoorMapper countySupportPoorMapper;

    @Autowired
    private CadreSupportPoorMapper cadreSupportPoorMapper;

    @Autowired
    private CmsSpecialMapper cmsSpecialMapper;

    @Autowired
    private CmsSpecialCommentMapper specialCommentMapper;

    @Autowired
    private CmsSpecialCommentUpMapper specialCommentUpMapper;

    /**
     *  扶贫县及其商品信息的取得
     *   爱心助力页面 banner以外的信息的取得
     * @return
     */
    @RequestMapping(value = "countyList",method = RequestMethod.GET)
    @ApiOperation(value = "扶贫县信息列表", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestEntity supportPoorCountyList(@RequestParam(required = false, defaultValue = "") String recommendType){
        try{
            Map<String, String> searchParam = new HashMap<String, String>();
            if (StringUtils.isEmpty(recommendType)) {
                searchParam.put("tableName", "bw_store s, bw_goods_common gc");
            } else {
                searchParam.put("tableName", "bw_store s, bw_goods_common gc, bw_active_area_goods aag");
                searchParam.put("recommendType", "axzl");
            }
            List<CountySupportPoor> countySupportPoorList = countySupportPoorMapper.selectCadreGoodsList(searchParam);
            return RestEntity.success(countySupportPoorList);
        }catch (Exception e){
            e.printStackTrace();
            return RestEntity.failed();
        }
    }

    /**
     *  扶贫专题的取得
     *   扶贫县概况的取得
     * @return
     */
    @RequestMapping(value = "specialInfo",method = RequestMethod.GET)
    @ApiOperation(value = "扶贫县概况信息", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestEntity supportPoorSpecialInfo(@RequestParam(required = true) Integer countyId){
        try{
            // 扶贫县信息的取得(扶贫县名称显示时用)
            CountySupportPoor countySupportPoor = countySupportPoorMapper.selectByPrimaryKey(countyId);

            // 扶贫专题的取得
            CmsSpecialWithBLOBs cmsSpecialWithBLOBs = new CmsSpecialWithBLOBs();
            cmsSpecialWithBLOBs.setPageSwitch(false);
            cmsSpecialWithBLOBs.setSpecialState(2);
            cmsSpecialWithBLOBs.setSpecialRegionId(countyId);
            List<CmsSpecialWithBLOBs> cmsSpecialWithBLOBsList = cmsSpecialMapper.selectByCondtion(cmsSpecialWithBLOBs);
            countySupportPoor.setSpecialList(cmsSpecialWithBLOBsList);

            return RestEntity.success(countySupportPoor);
        }catch (Exception e){
            e.printStackTrace();
            return RestEntity.failed();
        }
    }

    /**
     *  扶贫县干部信息的取得
     *   扶贫县人物介绍的取得
     * @return
     */
    @RequestMapping(value = "cadreInfo",method = RequestMethod.GET)
    @ApiOperation(value = "扶贫县干部信息", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestEntity supportPoorCadreInfo(@RequestParam(required = true) Integer countyId,
                                           @RequestParam(required = false, defaultValue = "0") Integer cadreId){
        try{

            // 扶贫县信息的取得(干部所在地区显示时用)
            CountySupportPoor countySupportPoor = countySupportPoorMapper.selectByPrimaryKey(countyId);

            // 扶贫县干部信息的取得
            List<CadreSupportPoor> cadreList = new ArrayList<CadreSupportPoor>();

            // 有cadreId时，只取的一个代言人
            if (cadreId != 0) {
                CadreSupportPoor cadreSupportPoor =  cadreSupportPoorMapper.selectByPrimaryKey(cadreId);
                cadreList.add(cadreSupportPoor);
            } else {
                // 没有cadreId时，取得所有的代言人
                CadreSupportPoor cadreSupportPoor = new CadreSupportPoor();
                cadreSupportPoor.setPageSwitch(false);
                cadreSupportPoor.setCountyId(countyId);
                cadreList =  cadreSupportPoorMapper.selectListByCondition(cadreSupportPoor);
            }

            countySupportPoor.setCadreList(cadreList);

            return RestEntity.success(countySupportPoor);
        }catch (Exception e){
            e.printStackTrace();
            return RestEntity.failed();
        }
    }

    /**
     * 扶贫采风
     * @param title
     * @param content
     * @return
     */
    @RequestMapping(value = "list",method = RequestMethod.POST)
    @ApiOperation(value = "扶贫风采列表", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestEntity supportPoorList(@RequestParam(required = false, defaultValue = "0") Integer start,
                                      @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                      String title,String content,Integer regionId,String year){
        try{
            CmsSpecialWithBLOBs cmsSpecialWithBLOBs = new CmsSpecialWithBLOBs();
            cmsSpecialWithBLOBs.setStart(start);
            cmsSpecialWithBLOBs.setPageSize(pageSize);
            cmsSpecialWithBLOBs.setSpecialState(2);
            cmsSpecialWithBLOBs.setSpecialTitle(StringUtils.isBlank(title) ? null : title);
            cmsSpecialWithBLOBs.setSpecialContent(StringUtils.isBlank(content) ? null : content);
            cmsSpecialWithBLOBs.setSpecialDate(StringUtils.isBlank(year)?null:year);
            cmsSpecialWithBLOBs.setSpecialRegionId(StringUtils.isBlank(regionId+"")?null:regionId);
            List<CmsSpecialWithBLOBs> cmsSpecialWithBLOBsList = cmsSpecialMapper.selectByCondtion(cmsSpecialWithBLOBs);
            return RestEntity.success(cmsSpecialWithBLOBsList);
        }catch (Exception e){
            e.printStackTrace();
            return RestEntity.failed();
        }
    }

    /**
     * 跳转到修改扶贫采风页面
     * @return
     */
    @RequestMapping(value = "view",method = RequestMethod.GET)
    @ApiOperation(value = "扶贫风采详情", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestEntity viewSupportPoor( @RequestParam(required = true) int id){
        try{
            CmsSpecialWithBLOBs cmsSpecialWithBLOBs = cmsSpecialMapper.selectByPrimaryKey(id);

            // 评论留言的取得
            CmsSpecialComment specialComment = new CmsSpecialComment();
            specialComment.setSpecialId(id);
            specialComment.setPageSwitch(false);
            List<CmsSpecialComment> specialCommentList = specialCommentMapper.selectListByCondition(specialComment);
            cmsSpecialWithBLOBs.setSpecialCommentList(specialCommentList);

            // 专题点赞个数
            int thumbCount = specialCommentUpMapper.getSpecialCommentCount(id);
            cmsSpecialWithBLOBs.setThumbCount(thumbCount);

            return RestEntity.success(cmsSpecialWithBLOBs);
        }catch (Exception e){
            e.printStackTrace();
            return RestEntity.failed();
        }
    }
}
